package com.example.xueshen.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xueshen.common.component.Util.MyPageUtil;
import com.example.xueshen.common.entity.PageData;
import com.example.xueshen.common.entity.Teacher;
import com.example.xueshen.common.service.TeacherService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class TeacherController {
    @Resource
    private TeacherService teacherService;
//    @GetMapping("/teacher")
//    public String teacher(Map<String ,Object> map){
//        List<Teacher> teachers = teacherService.getAll();
//        map.put("teachers",teachers);
//        return "student/teacher";
//    }
    @GetMapping("/teacher")
    public String getAll(PageData pageData, Map<String ,Object> map,@RequestParam(value = "search",required = false) String search){
        if(search==null){
            search = "undefined";
        }if(search.equals("undefined")){
            search = null;
        }
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();

        Page<Teacher> page = teacherService.selectBy(new Page<>(pageData.getCurrent(), pageData.getSize()),search==null?null: wrapper.like("teacher_name", search));
        if(page.getPages()!=0){
            int[] navigatePageNums = MyPageUtil.getNavigatePageNums((int) page.getCurrent(), (int) page.getPages(),3);
            map.put("pageNums",navigatePageNums);
        }
        if(search!=null){
             map.put("One",search);
        }
        map.put("page",page);
                List<Teacher> teachers = teacherService.getAll();
        map.put("teachers",teachers);
        return "student/teacher";
    }
    @GetMapping("/teacher/{teacherId}")
    public String toUpdate(@PathVariable Integer teacherId, Map<String ,Object> map){
        Teacher teacher = teacherService.getById(teacherId);
        map.put("teacher",teacher);
        return "student/teacher::addOrUpdate";
    }
    @GetMapping("/toAddOne")
    public String toadd( Map<String ,Object> map){
        map.put("teacher",null);
        return "student/teacher::addOrUpdate";
    }

    @PostMapping("/teachers")
    @ResponseBody
    public String add(Teacher teacher){
        boolean b = teacherService.toAdd(teacher);
        return b?"成功":"失败";
    }
    @PutMapping("/teachers")
    @ResponseBody
    public String update(Teacher teacher){
        boolean b = teacherService.update(teacher);
        return b?"成功":"失败";
    }
    @GetMapping("teacher/delete/{teacherId}")
    @ResponseBody
    public String delete(@PathVariable Integer teacherId,Map<String ,Object> map){

        boolean b = false;
        try {
            b = teacherService.deleteById(teacherId);
        } catch (Exception e) {
            return "失败";
        }
        return b?"成功":"失败";
    }
}
